﻿/**
 * @author caojj
 * @version 1.0.7.5
 * @history
 * 		1.0.0.1		# 一些细节bug @ 20080412
 * 		1.0.1.0		+ setPagerInf 设置inf 比如（总页数）
 * 		1.0.1.11	# 翻页后输入框中的页码造成混淆 @ 20080614
 * 		1.0.2.0		+ dom 引用 @ 20080614
 * 		//1.0.2.2		# IE中列表改变时页面输入框停留在原位置 ***
 * 		1.0.3.0		+ 用例和详细注解 @20080708
 * 		1.0.3.1		# 列页面内容$refreshPageContent后还需要重新刷新页码refershPageNo的bug @20080714
 * 		1.0.4.0		+ 设置分页数据信息setPageData @20080714
 * 		1.0.5.0		+ 自动设置页码输入maxLength
 * 		1.0.6.0		# 重整了一些逻辑，加入了pageInfo信息提示
 * 		1.0.7.0		+ 在GO按钮前加入显示信息的文本节点pageBtnInf	@20080910
 * 		1.0.7.2		# 当一次显示的页数大于总页数时列出页码的bug *
 * 
 * @plan
 * 		重整命名，数据信息设置为私有
 * 
 * @usage
 * 		var pagerCot = document.getElementById("pagerCot");
 * 		var pager = new FWK.UI.PagerUI(10, true, '第$2页 共$1页');
 *		pager.setPagerData(...);
 * 		pager.renderTo(pagerCot);
 * 
 * 		pager.$refreshPageContent = function() {
 * 			listItems(this.currentPage);
 * 		}
 * 
 * 		function listItems(pageNo) {
 * 			...
 * 			pager.setPagerData(...);
 * 			//pager.totalPage = parseInt(...);
 * 			//pager.refershPageNo();
 * 		}
 */

/*
 * 定义命名空间
 */
try {
	if(!FWK.UI){
		throw "ns";
	}
} catch (ex) {
	try{FWK}catch(e){FWK={}}
	FWK.UI = {}
}

/**
 * 分页组件类
 * @param {Int} disPage	一次列出页
 * @param {Boolean} autoHideNoData	内容不足翻页时自动隐藏控件
 * @param {String}	infoStr	pageInfo信息
 */
FWK.UI.PagerUI = function(disPage, autoHideNoData, infoStr){
	var SELF = this;
	this.dom = null;
	this.autoHideNoData=autoHideNoData;
	this.currentPage=0;
	this.totalPage=0;
	this.totalRecord=0;
	this.everyPage=10;
	this.disPage=(disPage&&(!isNaN(disPage)))?parseInt(disPage):5;
	this.midPage=Math.floor(this.disPage/2);
	this.infoStr = infoStr;
	
	this.setDisPage = function(num) {
		num=parseInt(num);
		SELF.disPage=num;
		SELF.midPage=Math.floor(this.disPage/2);
	}
	
//	var navPage=this.goPage;
	
	var pageFir=document.createElement("a");pageFir.href="javascript:;";pageFir.innerHTML="&nbsp;";pageFir.className="pageFir";pageFir.onclick=navFirst;pageFir.onfocus=doBlur;
	var pagePre=document.createElement("a");pagePre.href="javascript:;";pagePre.innerHTML="&nbsp;";pagePre.className="pagePre";pagePre.onclick=navPrevious;pagePre.onfocus=doBlur;
	
	var pageNum=document.createElement("span");pageNum.className="pageNum";
	
	var pageNxt=document.createElement("a");pageNxt.href="javascript:;";pageNxt.innerHTML="&nbsp;";pageNxt.className="pageNxt";pageNxt.onclick=navNext;pageNxt.onfocus=doBlur;
	var pageLst=document.createElement("a");pageLst.href="javascript:;";pageLst.innerHTML="&nbsp;";pageLst.className="pageLst";pageLst.onclick=navLast;pageLst.onfocus=doBlur;
	
	var pageIpt=document.createElement("input");pageIpt.type="text";pageIpt.className="pageIpt";pageIpt.onkeydown=jumpPage;pageIpt.value = 1;
	var pageBtnInf=document.createElement("span");pageBtnInf.className="pageBtnInf";
	var pageBtn=document.createElement("a");pageBtn.href="javascript:;";pageBtn.innerHTML="&nbsp;";pageBtn.className="pageBtn";pageBtn.onclick=navPage;pageBtn.onfocus=doBlur;

	var pageinf=document.createElement("span");pageinf.className="pageInfo";
	
	this.pageFir=pageFir;
	this.pagePre=pagePre;
	this.pageNum=pageNum;
	this.pageNxt=pageNxt;
	this.pageLst=pageLst;
	this.pageIpt=pageIpt;
	this.pageBtnInf=pageBtnInf;
	this.pageBtn=pageBtn;
	this.pageinf=pageinf;
	
	function navFirst(){
		if (SELF.currentPage > 1) {
			SELF.goPage(1);
		}
	};
	function navPrevious(){
		if (SELF.currentPage > 1) {
			SELF.goPage(SELF.currentPage - 1);
		}
	};
	function navNext(){
		if (SELF.currentPage < SELF.totalPage) {
			SELF.goPage(SELF.currentPage + 1);
		}
	};
	function navLast(){
		if (SELF.currentPage < SELF.totalPage) {
			SELF.goPage(SELF.totalPage);
		}
	};
	function navPage(){
		if (this.tagName) {
			doBlur.apply(this);
		}		
		
		var pgno = parseInt(pageIpt.value);
		if(!isNaN(pgno)) {
			if(pgno==SELF.currentPage) return;
			SELF.goPage(pgno);
		}
	}
	
	function jumpPage(ev) {
		var evt = ev||window.event;
		if(evt.keyCode=="13") {
			navPage();
		}
	}
	
	function doBlur() {
		this.blur();
	}
}

/**
 * 呈现到界面
 * @param {Dom} cot	Dom引用
 */
FWK.UI.PagerUI.prototype.renderTo=function(cot) {
	cot.appendChild(this.pageFir);
	cot.appendChild(this.pagePre);
	cot.appendChild(this.pageNum);
	cot.appendChild(this.pageNxt);
	cot.appendChild(this.pageLst);
	cot.appendChild(this.pageBtnInf);
	cot.appendChild(this.pageIpt);
	cot.appendChild(this.pageBtn);
	cot.appendChild(this.pageinf);
	
	this.dom = cot;
	
	this.refershPageNo();
	
}

/*
 * 页跳转
 */
FWK.UI.PagerUI.prototype.goPage=function(pgno) {
	//this.refershPageNo();
	pgno=parseInt(pgno);
	if(this.div_cpage<1||this.div_cpage>this.div_tpage||this.div_cpage==pgno) {return;}
	if(pgno<=1) {pgno=1;}
	if(pgno>=this.totalPage) {pgno=this.totalPage;}
	this.currentPage=pgno;
		
	this.$refreshPageContent(this.currentPage);	
	
	this.refershPageNo();
}

/**
 * 刷新分页控件
 * @param {Int} pgno		页码		//将来会废除，不推荐使用
 */
FWK.UI.PagerUI.prototype.refershPageNo=function(pgno) {
	pgno=parseInt(pgno);
	var SELF = this;
	if(pgno) {this.currentPage = parseInt(pgno);}
	this.pageNum.innerHTML="";
	if(!this.currentPage){
		this.currentPage=1;
	}
	
	var startNo, endNo;
	if(this.totalPage>0) {
		startNo=(this.currentPage-this.midPage>=1&&this.totalPage>this.disPage)?(this.currentPage-this.midPage):1;
		endNo=(this.currentPage+this.midPage<=this.totalPage&&this.totalPage>this.disPage)?(this.currentPage+this.midPage):this.totalPage;
		if(startNo==1&&this.totalPage>this.disPage){
			endNo=this.disPage;
		}
		if(endNo==this.totalPage&&this.totalPage>this.disPage){
			startNo=this.totalPage-(this.disPage-1);
		}
	} else {
		startNo=1;endNo=1;
	}
	
	
	for(var i=startNo;i<=endNo;i++) {
		var page = document.createElement("a");page.href="javascript:;";page.appendChild(document.createTextNode(i));
		if(i==this.currentPage) {
			page.className="pageSe";
		}else{
			page.onclick=navPage;
		}
		this.pageNum.appendChild(page);
	}
	//--------
	function navPage(){
		var pgno = parseInt(this.innerHTML);
		pgno=parseInt(pgno);
		SELF.goPage(pgno);

	}
	
	
	if(this.autoHideNoData&&this.totalPage<=1) {
		if (this.dom) {
			this.dom.style.display = "none";
		}		
	} else {
		if (this.dom) {
			this.dom.style.display = "";
		}		
	}
	
	this.pageIpt.maxLength = this.totalPage.toString().length;
	this.setPagerInf(this.infoStr);
	this.pageIpt.value = this.currentPage;
}

/**
 * 设置页信息
 * @param {String} text
 */
FWK.UI.PagerUI.prototype.setPagerInf=function(text){
	if (text) {
		this.pageinf.innerHTML = text.replace(/\$1/g, this.totalPage).replace(/\$2/g, this.currentPage).replace(/\$3/g, this.totalRecord).replace(/\$4/g, this.everyPage);		
	}	
//	this.pageinf.appendChild($CT(text));
}

/**
 * 刷新页内容 覆盖该函数
 * @param {Object} data
 */
FWK.UI.PagerUI.prototype.$refreshPageContent=function(data){
	var cpage = this.currentPage;
	alert("$refreshPageContent() ->"+cpage)
}

/**
 * 设置分页数据信息
 * @param {Object} totalPage
 * @param {Object} currentPage
 * @param {Object} totalRecord
 * @param {Object} pageSize
 */
FWK.UI.PagerUI.prototype.setPagerData = function(totalPage, currentPage, totalRecord, pageSize) {
	if (totalPage!=null&&totalPage!=undefined) {
		this.totalPage = parseInt(totalPage);
	}
	if (currentPage!=null&&currentPage!=undefined) {
		this.currentPage = parseInt(currentPage);
	}
	if (totalRecord!=null&&totalRecord!=undefined) {
		this.totalRecord = parseInt(totalRecord);
	}
	if (pageSize!=null&&pageSize!=undefined) {
		this.pageSize = parseInt(pageSize);
	}
	this.refershPageNo();
//	this.pageIpt.maxLength = this.totalPage.toString().length;
}

